package ert;


import static org.junit.Assert.assertTrue;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.snapshot.IBulletSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * Tests if RiceMusubi used the right bullet power.
 * @author Emerson Tabucol
 * Adapted code from TestDaCruzerMovement.java
 *
 */
public class TestRiceMusubiFirePower extends RobotTestBed {
  /** True if the robot fired a bullet. */
  boolean fired = false;
  /** True if the bullets power used if correct according to distance to the enemy. */
  boolean correctBulletPower = false;
  
  /**
   * Specifies that SittingDuck and DaCruzer are to be matched up in this test case.
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,ert.RiceMusubi";
  }
  
  /**
   * This test runs for 10 round.
   * @return The number of rounds. 
   */
  @Override
  public int getNumRounds() {
    return 10;
  }
  
  /**
   * After each turn, check if it fires a bullet and uses the right power.
   * @param event Info about the current state of the battle.
   */
  @Override 
  public void onTurnEnded(TurnEndedEvent event) {
    IRobotSnapshot robotSD = event.getTurnSnapshot().getRobots()[0];
    IRobotSnapshot robotRB = event.getTurnSnapshot().getRobots()[1];
    IBulletSnapshot[] bullets = event.getTurnSnapshot().getBullets();
 
    double power = 0;
    double dist = 0;
    //position of my robot RiceMusubi
    double xPosRB = robotRB.getX();
    //position of robot SittingDuck
    double xPosSD = robotSD.getX();
   
    int bulletsFired = bullets.length;
    
    if (bulletsFired != 0) {
      fired = true;
    }
    for (int i = 0;i < bulletsFired;i++) {
     
       power = bullets[i].getPower();
       dist = Math.abs(xPosRB - xPosSD);
      
       if (dist <= 100 && power == 3) {
         correctBulletPower = true;
       } 
       else if (dist >= 100 && dist <= 200 && power == 2) {
         correctBulletPower = true;
       } 
       else if (dist >= 200 && power == 1) {
         correctBulletPower = true;
       }
       
    }
  }
  
  /**
   * After the battle, check to see that the robot went into the center. 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue("Check if robot fired a bullet", fired);
    assertTrue("Check if bullet power used is correct", correctBulletPower);
    
  }

}
